<template>
  <div class="app-container">
    <el-calendar v-model="calendarDate">
      <!-- 这里使用的是 2.5 slot 语法，对于新项目请使用 2.6 slot 语法-->
      <template slot="dateCell" slot-scope="{ date, data }">
        <!-- <div class="header">
          <i class="el-icon-delete" @click="handledele(date, data)"></i>
        </div> -->
        <div class="box" @click="handleClick(date, data)">
          <p :class="data.isSelected ? 'is-selected' : ''">
            {{ data.day.split("-").slice(2).join("-") }}
          </p>
          <div v-if="calendarData">
            <div v-for="(item, index) in calendarData" :key="index">
              <div class="textContent" v-if="item.setDate == data.day">
                <div class="header">
                  <i
                    class="el-icon-delete"
                    @click.stop="handledele(date, data)"
                  ></i>
                </div>
                <div>
                  <p :class="data.isSelected ? 'is-selected' : ''">
                    数字仓计划收货数量:{{ item.takeCount }}
                  </p>
                  <p class="item2">数字仓预约收货数量:{{ item.overCount }}</p>
                  <p class="item3">预约比例:{{ item.percentage }}%</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </template>
    </el-calendar>
    <div class="optionItem">
      <div class="setwarehose">
        <el-select v-model="value" placeholder="仓库">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div class="setYear">
        <el-date-picker v-model="year" type="year" format="yyyy年" value-format="yyyy" placeholder="选择年">
        </el-date-picker>
        <el-date-picker v-model="month" type="month" format="MM月" value-format="MM" placeholder="选择月">
        </el-date-picker>
      </div>
      <div class="setNumber">
          <span class="text">收货数量：</span>
          <el-input placeholder="请输入内容" v-model="number" clearable>
          </el-input>
        </div>
        <el-button type="primary" size="mini" round @click="handlesetmonth"
          >提交</el-button
        >
    </div>
    <el-dialog
      :title="optTitle"
      :visible.sync="dialogFormVisible"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form :model="form">
        <el-form-item label="日期:" :label-width="formLabelWidth">
          <!-- <el-input v-model="form.name" autocomplete="off"></el-input> -->
          {{ formattedDate }}
        </el-form-item>
        <el-form-item label="数字仓:" :label-width="formLabelWidth">
          <el-input v-model="form.takeCount" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="danger" @click="delEvent">删除</el-button> -->
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEvent">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  addReceive,
  updateReceive,
  delReceive,
  setTakemonthList,
} from "@/api/system/receive";
export default {
  data() {
    return {
      options: [
        {
          value: "1",
          label: "数字仓",
        },
      ],
      value: "",
      // flag: 1,
      optTitle: "",
      calendarDate: "",
      form: {
        takeCount: null,
        id: null,
      },
      dialogFormVisible: false,
      formLabelWidth: "120px",
      formattedDate: null,
      calendarData: [],
      // 设置一个月提交一次收货数量
      year: "",
      month: "",
      number: "",
    };
  },
  created() {
    this.$nextTick(() => {
      const prevBtn = document.querySelector(
        ".el-calendar__button-group .el-button-group>button:nth-child(1)"
      );
      const nextBtn = document.querySelector(
        ".el-calendar__button-group .el-button-group>button:nth-child(3)"
      );
      const handleButtonClick = (e) => {
        console.log(e);
        const preDate = new Date(this.calendarDate);
        let year = preDate.getFullYear();
        let month = preDate.getMonth() + 1;
        let currDate = `${year}-${month}`;
        console.log(currDate);
        // let firstDay = new Date(year, month - 1, 2);
        // let formattedFirstDay = firstDay.toISOString().split("T")[0];
        // console.log(formattedFirstDay); // 输出格式为: 2023-08-01
        // let lastDay = new Date(year, month, 1);
        // let formattedLastDay = lastDay.toISOString().split("T")[0];
        // console.log(formattedLastDay); // 输出格式为: 2023-08-31
      };
      // 点击前一个月
      prevBtn.addEventListener("click", handleButtonClick);
      // 点击下一个月
      nextBtn.addEventListener("click", handleButtonClick);
      // 点击本月
      let nowBtn = document.querySelector(
        ".el-calendar__button-group .el-button-group>button:nth-child(2)"
      );
      nowBtn.addEventListener("click", (e) => {
        console.log(e, this.calendarDate);
        const currentDate = new Date();
        let year = currentDate.getFullYear();
        let month = currentDate.getMonth() + 1;
        let currDate = `${year}-${month}`;
        console.log(currDate);
        // let firstDay = new Date(year, month - 1, 2);
        // let formattedFirstDay = firstDay.toISOString().split("T")[0];
        // console.log(formattedFirstDay); // 输出格式为: 2023-08-01
        // let lastDay = new Date(year, month, 1);
        // let formattedLastDay = lastDay.toISOString().split("T")[0];
        // console.log(formattedLastDay); // 输出格式为: 2023-08-31
      });
    });
  },
  mounted() {
    this.getlistData();
  },
  methods: {
    // 设置一个月数量
    handlesetmonth() {
      setTakemonthList({
        year: this.year,
        month: this.month,
        number: this.number,
      }).then((res) => {
        console.log(res);
        this.getlistData();
      });
    },
    handledele(date, dataValue, e) {
      console.log(123, e);
      this.calendarData.map((item) => {
        if (item.setDate == dataValue.day) {
          this.$modal
            .confirm('是否确认删除日期为" ' + dataValue.day + '"的数据项？')
            .then(() => {
              delReceive(item.id)
                .then((res) => {
                  console.log(res);
                  this.getlistData();
                  this.$modal.msgSuccess("删除成功");
                })
                .catch((error) => {
                  console.log(error);
                });
            })
            .catch((error) => {
              console.log(error);
            });
        }
      });
    },
    getlistData() {
      getList().then((res) => {
        this.calendarData = res.data;
      });
    },

    handleClick(date, dataValue) {
      this.form.id = null;
      this.form.takeCount = null;
      const arr = this.calendarData.map((item) => {
        if (item.setDate == dataValue.day) {
          this.form.id = item.id;
          this.form.takeCount = item.takeCount;
        }
        return item.setDate;
      });
      this.dialogFormVisible = true;
      this.optTitle = arr.includes(dataValue.day)
        ? "修改收货数量"
        : "新增收货数量";
      console.log("flag", arr.includes(dataValue.day));
      this.formattedDate = dataValue.day;
    },
    // handleAddClick(date, dataValue) {
    //   console.log(11, date, dataValue);
    //   this.dialogFormVisible = true;
    //   this.optTitle = "新增收货数量";
    //   this.formattedDate = dataValue.day;
    //   this.form.takeCount = null;
    // },
    // handleupdateClick(date, dataValue) {
    //   console.log(this.flag);
    //   console.log(11, date, dataValue);
    //   this.dialogFormVisible = true;
    //   this.optTitle = "修改收货数量";
    //   this.formattedDate = dataValue.day;
    //   this.calendarData.filter((item) => { // 这里为什么用过滤？
    //     if (item.setDate == dataValue.day) {
    //       this.form.id = item.id;
    //       this.form.takeCount = item.takeCount;
    //     }
    //   });
    // },
    saveEvent() {
      let params = {
        id: this.form.id,
        setDate: this.formattedDate,
        takeCount: this.form.takeCount,
      };
      if (this.form.id == null) {
        addReceive(params).then((res) => {
          console.log(res);
          this.operation("新增成功");
          // this.$modal.msgSuccess("新增成功");
          // this.dialogFormVisible = false;
          // this.getlistData();
        });
      } else {
        // params.id = this.form.id
        updateReceive(params).then((res) => {
          console.log(res);
          this.operation("修改成功");
          // this.$modal.msgSuccess("修改成功");
          // this.dialogFormVisible = false;
          // this.getlistData();
        });
      }
    },
    //  新增 编辑成功后
    operation(msg) {
      this.$modal.msgSuccess(msg);
      this.dialogFormVisible = false;
      this.getlistData();
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  position: relative;
  padding: 6px;
}
.is-selected {
  color: #1989fa;
  font-size: 14px;
}
::v-deep .data-show {
  background: #9be2db;
  border: 5px double #fff;
  border-radius: 5px;
  width: 60% !important;
  margin: 0 auto;
}
::v-deep .el-calendar-table .el-calendar-day {
  height: 130px !important;
}
// .header {
//   float: right;
//   ::v-deep .el-icon-delete:before {
//     color: #141414;
//   }
// }
.box {
  position: relative;
  z-index: 0;
  font-weight: 700;
  font-size: 14px;
  width: 100%;
  height: 100%;
  .textContent {
    line-height: 25px;
    width: 100%;
    height: 110px;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 12px 3px 3px 16px;
    .header {
      position: absolute;
      top: -4%;
      left: 92%;
      z-index: 999;
      ::v-deep .el-icon-delete:before {
        color: #141414;
      }
    }
  }
}
.box .textContent .item1 {
  color: rgb(25, 27, 27);
}
.box .textContent .item2 {
  color: #4bcffa;
}
.box .textContent .item3 {
  color: #0984e3;
}
::v-deep .el-dialog {
  width: 28%;
}
::v-deep .el-input__inner {
  width: 100%;
}
::v-deep .el-dialog__body {
  padding: 15px 20px !important;
  width: 400px;
}
::v-deep .el-calendar__title {
  font-size: 20px;
}
::v-deep .el-button-group > .el-button:last-child {
  font-size: 14px;
}
::v-deep .el-button-group > .el-button:not(:first-child):not(:last-child) {
  font-size: 14px;
}
::v-deep .el-button-group > .el-button:first-child {
  font-size: 14px;
}
::v-deep .el-calendar__body {
  padding: 4px 20px 35px !important;
}
.optionItem {
  position: absolute;
  top: 15px;
  left: 160px;
  display: flex;
  .setYear {
    margin-left: 10%;
    display: flex;
    .el-date-editor.el-input{
      width: 120px;
      margin-right: 10px;
    }
  }
  .setNumber{
    margin-left: 10px;
    display: flex;
    align-items: center;
    .text{
      display: block;
      width: 120px;
    }
  }
  ::v-deep .el-button{
    width: 100px;
    margin-left: 10px;
  }
}
</style>